package com.example.ai.mcp;

import com.example.ai.embedding.SimilarSearchReq;
import com.example.ai.output.NLReq;
import io.modelcontextprotocol.client.McpSyncClient;
import lombok.extern.slf4j.Slf4j;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.client.advisor.SimpleLoggerAdvisor;
import org.springframework.ai.chat.model.ChatModel;
import org.springframework.ai.chat.model.ChatResponse;
import org.springframework.ai.mcp.SyncMcpToolCallbackProvider;
import org.springframework.ai.tool.ToolCallbackProvider;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/mcp")
public class McpClientController {

    @Autowired
    private List<McpSyncClient> mcpSyncClients;

    @Autowired
    private ChatModel chatModel;

    @Autowired
    SyncMcpToolCallbackProvider syncMcpToolCallbackProvider;

    @PostMapping("/client")
    public ResponseEntity<?> client(@RequestBody NLReq req) {
        ChatClient chatClient = ChatClient.builder(chatModel)
                .defaultAdvisors(new SimpleLoggerAdvisor())
                .defaultTools(syncMcpToolCallbackProvider)
                .build();
        ChatResponse response = chatClient.prompt()
                .user(req.getContent())
                .call()
                .chatResponse();
        log.info("response:{}", response);
        return  ResponseEntity.ok(response.getResult().getOutput().getText());
    }
}
